package com.yunwushanhe.student.service.impl;

import com.yunwushanhe.student.bean.req.PageReq;
import com.yunwushanhe.student.bean.req.ScoreUpdateReq;
import com.yunwushanhe.student.bean.ret.Result;
import com.yunwushanhe.student.bean.req.ScoreAddReq;
import com.yunwushanhe.student.bean.ret.ScoreAndStudentName;
import com.yunwushanhe.student.entity.Score;
import com.yunwushanhe.student.mapper.ScoreMapper;
import com.yunwushanhe.student.service.ScoreService;
import com.yunwushanhe.student.util.PageHelperUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class ScoreServiceImpl implements ScoreService {

    @Resource
    private ScoreMapper scoreMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> add(ScoreAddReq addReq) {
        Score score = new Score();
        BeanUtils.copyProperties(addReq,score);
        scoreMapper.insert(score);
        return Result.ok("增加成功！");
    }

    @Override
    public Result<List<ScoreAndStudentName>> selectPage(PageReq pageReq) {
        PageHelperUtil.startPage(pageReq.getPageNow(),pageReq.getPageSize());
        List<ScoreAndStudentName> list = scoreMapper.queryList(pageReq);
        PageHelperUtil.wrapPageData(list);
        return Result.ok(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> deleteById(Long id) {
        scoreMapper.deleteById(id);
        return Result.ok("删除成功！");
    }

    @Override
    public Result<ScoreAndStudentName> selectById(Long id) {
        Score score = scoreMapper.selectById(id);
        ScoreAndStudentName data = new ScoreAndStudentName();
        BeanUtils.copyProperties(score,data); // 这里是赋值不全的
        return Result.ok(data);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> updateById(ScoreUpdateReq updateReq) {
        Score score = new Score();
        BeanUtils.copyProperties(updateReq,score);
        scoreMapper.updateById(score);
        return Result.ok("更新成功！");
    }
}
